package name.huzhenbo.java.objects.staticfactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Service provider framework, including 4 elements: 1. service interface, 2. provider interface,
 * 3. provider registration API, 4. service access API.
 * <p/>
 * e.g. Java Database Connectivity API (JDBC)
 * <p/>
 * In the case of JDBC, Connection plays the part of the service interface, DriverManager.registerDriver
 * is the provider registration API, DriverManager.getConnection is the service access API, and Driver
 * is the service provider interface.
 */
class Services {
    public static final String DEFAULT_SERVICE = "<def>";
    private static Map<String, Provider> providers = new ConcurrentHashMap<String, Provider>();

    public static Provider getInstance(String providerName) {
        Provider provider = providers.get(providerName);
        if (provider == null) {
            throw new IllegalArgumentException("The provider with name: " + providerName + " does not exist!");
        }
        return provider;
    }

    public static void registerProvider(String providerName, Provider provider) {
        providers.put(providerName, provider);
    }
}
